#!/usr/bin/env bash

# Copyright: 2016 - Stefano Mazzucco <stefano AT curso DOT re>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

NAME=$(basename $0)

INTERNAL=LVDS1
EXTERNAL=HDMI1
CONFIG_FILE="$HOME/.config/switch-monitor.conf"

usage()
{
    cat <<EOF
    Usage:
    $NAME [[-i | --internal] [-e | --external] [-l | --left] [-r | --right] [-a | --above] [-b | --below] [-c | --clone]]
    Display the internal monitor, an external projector, both or extend the internal monitor to the external one (left, right, above, below).
    See xrandr for more information.
    If present, $CONFIG_FILE will be sourced.
    The following variables will be used:
    INTERNAL - string that identifies the internal monitor (currently set to $INTERNAL).
    EXTERNAL - string that identifies the external monitor (currently set to $EXTERNAL)
    EXTERNAL_EXTRA_ARGS - extra arguments passed to xrandr for the external monitor.
    INTERNAL_EXTRA_ARGS - extra arguments passed to xrandr for the internal monitor.
EOF

}

check_monitor() {
    xrandr 2>/dev/null | grep -c "$1" &>/dev/null
    return $?
}


[[ -f $CONFIG_FILE ]] && . "$CONFIG_FILE"

FAIL=0
if ! check_monitor $INTERNAL; then
   echo "Internal monitor $INTERNAL not found"
   FAIL=$((FAIL+1))
fi
if ! check_monitor $EXTERNAL; then
    echo "External monitor $EXTERNAL not found"
    FAIL=$((FAIL+1))
fi
[[ $FAIL -ne 0 ]] && usage && exit 127

if [ $# -gt 0 ]; then
    case "$1" in
	-i | --internal)
            xrandr --output $EXTERNAL --off \
                && xrandr --output $INTERNAL --auto $INTERNAL_EXTRA_ARGS
            ;;
	-e | --external)
            xrandr --output $EXTERNAL --auto $EXTERNAL_EXTRA_ARGS \
                && xrandr --output $INTERNAL --off
            ;;
        -c | --clone)
            xrandr --output $INTERNAL --auto $INTERNAL_EXTRA_ARGS \
                && xrandr --output $EXTERNAL --auto $EXTERNAL_EXTRA_ARGS
            ;;
        -r | --right)
            xrandr --output $INTERNAL --auto $INTERNAL_EXTRA_ARGS \
                && xrandr --output $EXTERNAL $EXTERNAL_EXTRA_ARGS --auto --right-of $INTERNAL
            ;;
        -l | --left)
            xrandr --output $INTERNAL --auto $INTERNAL_EXTRA_ARGS \
                && xrandr --output $EXTERNAL $EXTERNAL_EXTRA_ARGS --auto --left-of $INTERNAL
            ;;
        -a | --above)
            xrandr --output $INTERNAL --auto $INTERNAL_EXTRA_ARGS \
                && xrandr --output $EXTERNAL $EXTERNAL_EXTRA_ARGS --auto --above $INTERNAL
            ;;
        -b | --below)
            xrandr --output $INTERNAL --auto $INTERNAL_EXTRA_ARGS \
                && xrandr --output $EXTERNAL $EXTERNAL_EXTRA_ARGS --auto --below $INTERNAL
            ;;
	*)
            usage
            ;;
    esac
else
    usage
fi

exit 0
